﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LView.CMS.ViewModels;
using Microsoft.AspNetCore.Mvc;
using LView.CMS.IServices;
using LView.CMS.Core.Helper;
using Microsoft.AspNetCore.Hosting;

namespace LView.CMS.Site.Controllers
{
    public class AssetsController : Controller
    {
        private readonly IAssetsService _assetsService;
        public AssetsController(IAssetsService assetsService)
        {
            _assetsService = assetsService;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [Route("/Assets/Add")]
        public async Task<string> AddAsync([FromForm] CourseAddOrModify input)
        {
            var (uploadResult, file) = await _assetsService.AddAssetsAsync(input);
            return JsonHelper.ObjectToJSON(uploadResult);
        }

        /// <summary>
        /// 上传更换头像
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadImage()
        {
            var imgFile = Request.Form.Files[0];
            var (resultCode, resultMsg, imageSrc) = _assetsService.UploadImage(imgFile);
            return Json(new { code = resultCode, msg = resultMsg, src = imageSrc });
        }
    }
}
